#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
using std::cerr;
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::ifstream;
using std::ofstream;
using std::istringstream;


class Dictionary
{
public:
    void read(const string &filename)
    {
        ifstream ifs(filename);

        if(!ifs)
        {
            cerr << "ifstream open file failed" << endl;
            return;
        }

        string line;
        while(std::getline(ifs,line))
        {
            istringstream iss(line);
            string word;
            while(iss >> word)
            {
                //处理单词
                dealWord(word);

                //尝试插入vector
                insert(word);
            }
        }

        ifs.close();
    }

    void dealWord(string & word)
    {
        auto it = word.begin();
        while(it != word.end())
        {
            if(!isalpha(*it))
            {
                it = word.erase(it);
            }else{
                ++it;
            }
        }
    }

    void insert(const string & word)
    {
        if(word.size() == 0)
        {
            return;
        }

        ++_dict[word];

    }

    void store(const string &filename)
    {
        ofstream ofs(filename);

        if(!ofs)
        {
            cerr << "ofstream open file failed" << endl;
            return;
        }

        for(auto & pa : _dict)
        {
            ofs << pa.first << " "
                << pa.second << endl;
        }

        ofs.close();
    }
private:
    map<string,int> _dict;
};

void test0(){
    Dictionary d1;
    d1.read("The_Holy_Bible.txt");
    d1.store("dict.dat");

}

int main(void){
    test0();
    return 0;
}
